package com.dji.GSDemo.GaodeMap.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.dji.GSDemo.GaodeMap.MApplication;


public class MyLog{

    private final String filename = "my_log.text";
    private final Context context = MApplication.getContext();

    private final ArrayList<MyLogListener> listeners = new ArrayList<MyLogListener>();

    private static class Singleton {
        private static final MyLog instance = new MyLog();
    }

    public static MyLog getInstance() {
        return MyLog.Singleton.instance;
    }

    private MyLog() {}

    public interface MyLogListener {
        public void onLog(String msg);
        public void onStore(String fileContents);
        public void onRead(String fileContents);
    }

    public void addListener(MyLogListener listener){
        listeners.add(listener);
    }

    public void removeListener(MyLogListener listener){
        listeners.remove(listener);
    }

    public void tempLog(String msg){
        for(MyLogListener listener:listeners){
            listener.onLog(msg);
        }
    }

    public void log(String msg){
        try {
            FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE + Context.MODE_APPEND);
            fos.write(msg.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            Log.e("30000", e.getMessage());
        }
        for(MyLogListener listener:listeners){
            listener.onLog(msg);
        }
    }

    public void store(String fileContents){
        try {
            FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
            fos.write(fileContents.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            Log.e("30000", e.getMessage());
        }
        for(MyLogListener listener:listeners){
            listener.onStore(fileContents);
        }
    }

    public String read(){
        String contents = "";
        StringBuilder stringBuilder = new StringBuilder();
        try {
            FileInputStream fis = context.openFileInput(filename);
            InputStreamReader inputStreamReader =
                    new InputStreamReader(fis, StandardCharsets.UTF_8);
            BufferedReader reader = new BufferedReader(inputStreamReader);
            String line = reader.readLine();
            while (line != null) {
                stringBuilder.append(line).append('\n');
                line = reader.readLine();
            }
        } catch (IOException e) {
            // Error occurred when opening raw file for reading.
        } finally {
            contents = stringBuilder.toString();
        }
        for(MyLogListener listener:listeners){
            listener.onRead(contents);
        }
        return contents;
    }

}
